/**
 * 2009/07/16 
 * @author Manu Francis Mathew
 */
public class WaterReticulationHead {
    
    double branchNodes = 100d;
    double loopNodes = 100d;
    double demandNodes = 200d;
    
    private void printUsage(){
        System.out.println("\n Usage \t\tjava WaterReticulationHead branchNodes loopNodes \n For example\tjava WaterReticulationHead 120 \t\t 80\n\n Both numbers should be in the range of 0 to 200\n Sum of both arguments should be between 100 and 200");
    }
    private double computeMaxFlow(){
        //convert below val to m3ps
        //System.out.println("In method computeMaxFlow ");
        double q = 0d;
        q = (double)(((20d/60d)*.001d)*(branchNodes+loopNodes));
        //System.out.println("Maximum total flow = "+ q + " litres per second ");
        return q;

    }


    private double computeMaximumHeadLoss(){
        //System.out.println("In method computeMaximumHeadLoss");
        if(branchNodes==0&&loopNodes==0){
            return 15;
        }
        if((branchNodes+loopNodes) >200 || (branchNodes+loopNodes) <100 ){
            printUsage();
        }
        double qMax = computeMaxFlow();
        double D = 0.2d;
        //remember to add computed value before returning
        double pHL =0d; //variable across each pipe link
        double L=10d;
        double kinematicViscosity=0.00000195d;
        double R=3000d; //variable across each pipe link
        double A = 3.14d*(D/2d)*(D/2d);
        double q =0d;    // variable across each pipe link
        double vel =0d ; // variable across each pipe link
        R=vel*D/kinematicViscosity;
        double friction = 0.316d/(Math.sqrt(Math.sqrt(R)));
        pHL=((friction*L*vel*vel/2d)/9.81d)/D;
        //above pHL to be computed for all links based on variable q,R,vel,L=10.
        double tHL = 0d;
        double qNode= qMax/(branchNodes+loopNodes);
	if(loopNodes!=0){
        for(int i=0; i<loopNodes ; i+=2){
            q=(qMax -i*qNode)/2d;
            vel=q/A;
            //System.out.println("Velocity = "+vel);
            R=vel*D/kinematicViscosity;
	    //System.out.println(R+"\t");
            //System.out.println("Reynolds number = "+R);
	    if(R < 2000)
	    {
		friction = 64d/R;	
	    }else
	    {
            	friction = 0.316d/Math.sqrt(Math.sqrt(R));
	    }
            //System.out.println("Friction as per Blasius equation = "+vel);
            pHL= ((2d*friction*L*vel*vel/2d)/9.81d)/D;
            //System.out.println("Head loss as per Darcy W equation = " + pHL);
            tHL+=2d*pHL;
            //System.out.println("tHL for loop = " + tHL);
        }}
	if(loopNodes!=0){
        qMax=q;
	}
        for(int i=0; i< (int)branchNodes/2;i+=2){
            q=qMax-2*qNode;
            vel=q/A;
            R=vel*D/kinematicViscosity;
            //System.out.println(R + "\t");
            if(R < 2000)
	    {
		friction = 64d/R;	
	    }else
	    {
            	friction = 0.316d/Math.sqrt(Math.sqrt(R));
	    }
            pHL= ((2d*friction*L*vel*vel/2d)/9.81d)/D;
            //System.out.println(pHL);
            tHL+=pHL;
        }
        double branchLinkHL=0d;
        q=qNode;
        vel=q/A;
        R=vel*D/kinematicViscosity;
	//System.out.println(R+"\t");
            if(R < 2000)
	    {
		friction = 64d/R;	
	    }else
	    {
            	friction = 0.316d/Math.sqrt(Math.sqrt(R));
	    }
        branchLinkHL= ((friction*L*vel*vel/2d)/9.81d)/D;
        //System.out.println("branchLinkHL = "+branchLinkHL);
        tHL +=  branchLinkHL*branchNodes;
        return 15d+tHL;
    }
    public WaterReticulationHead(String[] args)
    {
        if(args.length!=2)
        {
            printUsage();
            System.exit(1);// modify this exit code
        }
        branchNodes = Double.parseDouble(args[0].trim());
        loopNodes = Double.parseDouble(args[1].trim());
        //System.out.println("Branch Nodes = " + branchNodes);
        //System.out.println("Loop Nodes = " + loopNodes);
        double htRqd = computeMaximumHeadLoss();
        System.out.println("Tank Base Height For \n"+branchNodes+" branchNodes and "+loopNodes+ " loopNodes = "+htRqd+" m ");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new WaterReticulationHead(args);
    }

}
